package com.choice;

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class enroll_q_cal extends Activity implements OnItemClickListener, OnClickListener
{
	public static int SUNDAY 		= 1;
	public static int MONDAY 		= 2;
	public static int TUESDAY 		= 3;
	public static int WEDNSESDAY 	= 4;
	public static int THURSDAY 		= 5;
	public static int FRIDAY 		= 6;
	public static int SATURDAY 		= 7;
	int curDay;
	int mHour;
    int mMinute;
    int count_i=0;
	static final int TIME_DIALOG_ID = 1;
	
	private TextView mTvCalendarTitle;
	private TextView mTVCalenaarTitle_month;
	private TextView mTVCalendar_select_date;
	private GridView mGvCalendar;
	private ImageButton mImageButton_plus;
	private Button nextBtn;
	
	private ArrayList<DayInfo> mDayList;
	private CalendarAdapter mCalendarAdapter;
	public SellectDate[] sellect_date_array = new SellectDate[4];
	public String[] sellect_string_data = new String[4];
	
	Calendar mLastMonthCalendar;
	Calendar mThisMonthCalendar;
	Calendar mNextMonthCalendar;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.enroll_q_cal);
		
		Button bLastMonth = (Button)findViewById(R.id.gv_calendar_activity_b_last);
		Button bNextMonth = (Button)findViewById(R.id.gv_calendar_activity_b_next);
		
		nextBtn = (Button) findViewById(R.id.enroll_q_cal_next_btn);
		
		mTvCalendarTitle = (TextView)findViewById(R.id.gv_calendar_activity_tv_title);
		mGvCalendar = (GridView)findViewById(R.id.gv_calendar_activity_gv_calendar);
		mTVCalenaarTitle_month = (TextView) findViewById(R.id.gv_calendar_activity_tv_title_month);
		mTVCalendar_select_date = (TextView) findViewById(R.id.gv_calendar_activity_tv_select_date);
		
		mImageButton_plus = (ImageButton) findViewById(R.id.gv_calendar_activity_imgbt_plus);
		
		bLastMonth.setOnClickListener(this);
		bNextMonth.setOnClickListener(this);
		
		for(int i=0; i<4; i++){
			sellect_date_array[i] = new SellectDate();
			sellect_string_data[i] = "";
		}
		
		mImageButton_plus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				sellect_date_array[count_i].setSYear(mThisMonthCalendar.get(Calendar.YEAR));
				sellect_date_array[count_i].setSMonth((mThisMonthCalendar.get(Calendar.MONTH)+1));
				sellect_date_array[count_i].setSDay(curDay);
				sellect_date_array[count_i].setSHour(mHour);
				sellect_date_array[count_i].setSMinute(mMinute);
				sellect_string_data[count_i] = (String) mTVCalendar_select_date.getText();
				Toast.makeText(enroll_q_cal.this,"버튼이눌렸습니다.", Toast.LENGTH_SHORT).show();
				count_i++;
			}
		});
		
		
		nextBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(enroll_q_cal.this, enroll_preview_cal.class);
				//intent.putExtra("data", sellect_string_data);
				startActivity(intent);
			}
		});
		
		mDayList = new ArrayList<DayInfo>();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume()
	{
		super.onResume();
		
		// 이번달 의 캘린더 인스턴스를 생성한다.
		mThisMonthCalendar = Calendar.getInstance();
		mThisMonthCalendar.set(Calendar.DAY_OF_MONTH, 1);
		getCalendar(mThisMonthCalendar);
	}

	/**     
	 * 달력을 셋팅한다.  
	 * @param calendar 달력에 보여지는 이번달의 Calendar 객체   
	 */
	private void getCalendar(Calendar calendar)
	{
		int lastMonthStartDay;
		int dayOfMonth;
		int thisMonthLastDay;
		
		mDayList.clear();
		
		// 이번달 시작일의 요일을 구한다. 시작일이 일요일인 경우 인덱스를 1(일요일)에서 8(다음주 일요일)로 바꾼다
		dayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);
		thisMonthLastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		calendar.add(Calendar.MONTH, -1);
		Log.e("지난달 마지막일", calendar.get(Calendar.DAY_OF_MONTH)+"");

		// 지난달의 마지막 일자를 구한다.
		lastMonthStartDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		calendar.add(Calendar.MONTH, 1);
		Log.e("이번달 시작일", calendar.get(Calendar.DAY_OF_MONTH)+"");
		
		if(dayOfMonth == SUNDAY)
		{
			dayOfMonth += 7;
		}
		
		lastMonthStartDay -= (dayOfMonth-1)-1;
		

		 // 캘린더 타이틀(년월 표시)을 세팅한다. 
		mTvCalendarTitle.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년 ");
		mTVCalenaarTitle_month.setText((mThisMonthCalendar.get(Calendar.MONTH)+1)+"월");

		DayInfo day;
		
		Log.e("DayOfMOnth", dayOfMonth+"");
		
		for(int i=0; i<dayOfMonth-1; i++)
		{
			int date = lastMonthStartDay+i;
			day = new DayInfo();
			day.setDay(Integer.toString(date));
			day.setInMonth(false);
			
			mDayList.add(day);
		}
		for(int i=1; i <= thisMonthLastDay; i++)
		{
			day = new DayInfo();
			day.setDay(Integer.toString(i));
			day.setInMonth(true);
			
			mDayList.add(day);
		}
		for(int i=1; i<42-(thisMonthLastDay+dayOfMonth-1)+1; i++)
		{
			day = new DayInfo();
			day.setDay(Integer.toString(i));
			day.setInMonth(false);
			mDayList.add(day);
		}
		
		initCalendarAdapter();
	}

	 /**    
	  * 지난달의 Calendar 객체를 반환합니다.    
	  * @param calendar     
	  *  @return LastMonthCalendar     
	  */
	private Calendar getLastMonth(Calendar calendar)
	{
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		calendar.add(Calendar.MONTH, -1);
		mTvCalendarTitle.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년 ");
		mTVCalenaarTitle_month.setText((mThisMonthCalendar.get(Calendar.MONTH)+1)+"월");
		return calendar;
	}

	/**  
	 * 다음달의 Calendar 객체를 반환합니다.     
	 * @param calendar    
	 * @return NextMonthCalendar     
	 */
	private Calendar getNextMonth(Calendar calendar)
	{
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		calendar.add(Calendar.MONTH, +1);
		mTvCalendarTitle.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년");
		mTVCalenaarTitle_month.setText((mThisMonthCalendar.get(Calendar.MONTH)+1)+"월");
		return calendar;
	}
	
	public void onItemClick(AdapterView<?> parent, View v, int position, long arg3)
	{
		
	}
	
	public void onClick(View v)
	{
		switch(v.getId())
		{
		case R.id.gv_calendar_activity_b_last:
			mThisMonthCalendar = getLastMonth(mThisMonthCalendar);
			getCalendar(mThisMonthCalendar);
			break;
		case R.id.gv_calendar_activity_b_next:
			mThisMonthCalendar = getNextMonth(mThisMonthCalendar);
			getCalendar(mThisMonthCalendar);
			break;
		}
	}
	
	//TimePicker 由ъ���    
    private OnTimeSetListener mTimeSetListener = new OnTimeSetListener() {
		
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			mHour = hourOfDay;
			mMinute = minute;
			mTVCalendar_select_date.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년"+(mThisMonthCalendar.get(Calendar.MONTH)+1)+"월"+ curDay+"일"+mHour+"시"+mMinute+"분");
		}
	};
 
	protected Dialog onCreateDialog(int id){
		
		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
		default:
			break;
		}
		return null;
	}

	private void initCalendarAdapter()
	{
		mCalendarAdapter = new CalendarAdapter(this, R.layout.day, mDayList);
		mGvCalendar.setAdapter(mCalendarAdapter);
		
		mGvCalendar.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				DayInfo curItem = (DayInfo)mCalendarAdapter.getItem(position);
                curDay = Integer.valueOf(curItem.getDay());
                //Log.d("CalendarMonthViewActivity", "Selected : " + curDay);
				//Toast.makeText(GVCalendarActivity.this,Integer.toString(curDay), Toast.LENGTH_SHORT).show();
				showDialog(TIME_DIALOG_ID);
				//mTVCalendar_select_date.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년"+(mThisMonthCalendar.get(Calendar.MONTH)+1)+"월"+ curDay+"일");
			
			}
		});		
	}
}